//-----------------------------------------------------------------------
// <copyright file="CKS.Office365.EventReceiver.cs" company="CKS.Office365">
// CKS Office365 CodePlex
// </copyright>
//-----------------------------------------------------------------------

namespace CKS.Office365.Prerequisite.Features
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using CKS.Office365.Diagnostics;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("0739839e-1119-4035-a403-288bb71f932d")]
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
    public class CKSOffice365EventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Applies the prerequistes constraints needed for CKS.Office365, this includings renaming lists and libraries to allow the 
        /// publishing feature to be actived.
        /// </summary>
        /// <param name="properties">The properties.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }
            
            SPSite site = properties.Feature.Parent as SPSite;

            Log log = new Log(site);
            log.Write("CKS.Office365.Prerequisites feature activating.", string.Empty, string.Empty, LoggingLevel.Information);

            string instanceId = Guid.NewGuid().ToString();

            Navigation.ClearQuickLaunch(site.RootWeb);

            Backup.Folders(site.RootWeb, instanceId);

            Backup.Lists(site.RootWeb, instanceId);

            log.Write("CKS.Office365.Prerequisites feature activated.", string.Empty, string.Empty, LoggingLevel.Information);
        }
    }
}
